var funcIndex = 0;
var isInIframe = true;

function getCrossCallback(func) {
    funcIndex++;

    var i = funcIndex.toString();

    crossCallback[i] = func;

    return i;
}


var backupSetkey=WebUIJS.SetActiveMenuKey;
var backupRefreshEvent=WebUIJS.RefreshEvent;

WebUIJS.RefreshEvent = function(collection, onsuccess) {
    backupRefreshEvent(collection, onsuccess);

    $("a[target=_blank]").each(function() {
        $(this).click(function() {
            var link = this;
            var href = $(link).attr("href");

            $(link).removeAttr("href");
            $(link).data("link", href);

            WebUIJS.CrossCall("addtaburl",
                {
                    Url: href
                });
            return true;
        });
    });

    $("a[target=_top]").each(function () {
        $(this).click(function () {
            var link = this;
            var href = $(link).attr("href");

            var win = window.open(href, "_blank");
           // win.focus();

            return false;
        });
    });
};

WebUIJS.RefreshIFrameHeight = function() {
    WebUIJS.CrossCall("Resize", {
        Height: $("body").outerHeight()
    });
}

WebUIJS.GoTo = function (url, sender, target, isManage) {
  
    if (!target) {
        target = "_self";
    }

    if (typeof isManage == 'undefined' || isManage == null) {
        isManage = true;
    }

    if (target === "_blank") {
        if (isManage) {
            WebUIJS.CrossCall("addtaburl",
                {
                    Url: url
                });
        }
        else {
            var win = window.open(url, target);
            win.focus();
            return;
        }

        
        return true;
    } else {
        
    }

    if (target != "" && target != "_self") {
        var win = window.open(url, target);
        win.focus();
        return;
    }

    if (sender !== undefined && sender !== null) {
        var closestEl = $(sender).closest(".layui-layer");

        if (closestEl.hasClass('layui-layer')) {
            if (closestEl.find(".datapicker").length > 0) {
                return;
            }

            if (closestEl.find(".modalwindow").length > 0) {
                WebUIJS.ModalByUrl(url, null,
                    function () {
                        var index = $(closestEl).attr("times");
                        layer.close(index);
                    });
            }

        } else {
            window.location.href = url;
        }
    } else {
        window.location.href = url;
    }


}


WebUIJS.SetActiveMenuKey=function (menuKey){
    backupSetkey(menuKey);
    WebUIJS.CrossCall("SetMenuKey", { MenuKey: menuKey });
}

WebUIJS.CrossCall = function (funcName, args, callback) {
    var callbackFuncID =0;// getCrossCallback(callback);

    var json = {
        Message: "Call",
        FuncName: funcName,
        Args: args,
        Callback: {
            FuncID: callbackFuncID
        }
    }

    if (callbackFuncID > 0) {
        json.Callback = {
            FuncID: callbackFuncID
        }
    }

    window.parent.postMessage(JSON.stringify(json), "*");
}

$(document).ready(function(){
    WebUIJS.CrossCall("SetTitle", {
        Title: document.title
    });

    WebUIJS.CrossCall("Resize", {
        Height: $("body").outerHeight()
    });

    WebUIJS.on("location.urlchange", function (data) {
        WebUIJS.CrossCall("SetUrl", { Url: data.newUrl });
    })
    //WebUIJS.CrossCall("SetMenuKey", { MenuKey: currentMenuKey });
})